package cn.zhx.xa.system.controller;

import cn.zhx.xa.model.dto.user.SysUserAddOrUpdateDto;
import cn.zhx.xa.model.dto.user.SysUserSearchDto;
import cn.zhx.xa.model.dto.user_role.AssignRoleDto;
import cn.zhx.xa.model.entity.SysUser;
import cn.zhx.xa.model.vo.common.Result;
import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import cn.zhx.xa.system.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api/system/user")
@Tag(name = "用户管理" , description = "用户管理接口")
public class SysUserController {

    @Resource
    private SysUserService sysUserService ;

    @PostMapping(value = "/auth/findByPage/{pageNum}/{pageSize}")
    @Operation(summary = "分页查询用户信息")
    public Result<Page<SysUser>> findByPage(@RequestBody SysUserSearchDto sysUserSearchDto ,
                                            @PathVariable(value = "pageNum") Integer pageNum ,
                                            @PathVariable(value = "pageSize") Integer pageSize) {
        Page<SysUser> pageInfo = sysUserService.findByPage(sysUserSearchDto , pageNum , pageSize) ;
        return Result.build(pageInfo , ResultCodeEnum.SUCCESS) ;
    }

    @PostMapping(value = "/auth/saveSysUser")
    @Operation(summary = "新增用户")
    public Result saveSysUser(@Valid @RequestBody SysUserAddOrUpdateDto sysUserAddOrUpdateDto) {
        sysUserService.saveSysUser(sysUserAddOrUpdateDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @PutMapping(value = "/auth/updateSysUser")
    @Operation(summary = "修改用户")
    public Result updateSysUser(@Valid @RequestBody SysUserAddOrUpdateDto sysUserAddOrUpdateDto) {
        sysUserService.updateSysUser(sysUserAddOrUpdateDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

    @DeleteMapping(value = "/auth/deleteById/{userId}")
    @Operation(summary = "删除用户")
    public Result deleteById(@PathVariable(value = "userId") Long userId) {
        sysUserService.deleteById(userId) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }


    @PostMapping("/auth/doAssign")
    @Operation(summary = "保存用户的角色信息")
    public Result doAssign(@Valid @RequestBody AssignRoleDto assignRoleDto) {
        sysUserService.doAssign(assignRoleDto) ;
        return Result.build(null , ResultCodeEnum.SUCCESS) ;
    }

}